class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        std::unordered_map<int, int> m;
        int sum = 0;
        int count = 0;
        m[sum]++;
        for (int i = 0; i < nums.size(); i++)
        {
            int pre = sum + nums[i];
            count += m[pre - k];
            m[pre]++;
            sum = pre;
        }
        return count;
    }
};